package com.ktjy.ruoyidata.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktjy.ruoyidata.mapper.MyWorkshopMapper;
import com.ktjy.ruoyidata.domain.MyWorkshop;
import com.ktjy.ruoyidata.service.IMyWorkshopService;

/**
 * 车间Service业务层处理
 *
 * @author t382
 * @date 2025-05-06
 */
@Service
public class MyWorkshopServiceImpl implements IMyWorkshopService {
    @Autowired
    private MyWorkshopMapper myWorkshopMapper;

    /**
     * 查询车间
     *
     * @param workshopId 车间主键
     * @return 车间
     */
    @Override
    public MyWorkshop selectMyWorkshopByWorkshopId(Long workshopId) {
        return myWorkshopMapper.selectMyWorkshopByWorkshopId(workshopId);
    }

    /**
     * 查询车间列表
     *
     * @param myWorkshop 车间
     * @return 车间
     */
    @Override
    public List<MyWorkshop> selectMyWorkshopList(MyWorkshop myWorkshop) {
        return myWorkshopMapper.selectMyWorkshopList(myWorkshop);
    }

    /**
     * 新增车间
     *
     * @param myWorkshop 车间
     * @return 结果
     */
    @Override
    public int insertMyWorkshop(MyWorkshop myWorkshop) {
        // 生成车间编码
        String prefix = "WS"; // 默认

        // 生成3位随机数
        String randomPart = String.format("%03d", new Random().nextInt(1000));

        String clientCode = prefix + randomPart;
        myWorkshop.setWorkshopCode(clientCode);
        myWorkshop.setCreateTime(DateUtils.getNowDate());
        return myWorkshopMapper.insertMyWorkshop(myWorkshop);
    }

    /**
     * 修改车间
     *
     * @param myWorkshop 车间
     * @return 结果
     */
    @Override
    public int updateMyWorkshop(MyWorkshop myWorkshop) {
        myWorkshop.setUpdateTime(DateUtils.getNowDate());
        return myWorkshopMapper.updateMyWorkshop(myWorkshop);
    }

    /**
     * 批量删除车间
     *
     * @param workshopIds 需要删除的车间主键
     * @return 结果
     */
    @Override
    public int deleteMyWorkshopByWorkshopIds(Long[] workshopIds) {
        return myWorkshopMapper.deleteMyWorkshopByWorkshopIds(workshopIds);
    }

    /**
     * 删除车间信息
     *
     * @param workshopId 车间主键
     * @return 结果
     */
    @Override
    public int deleteMyWorkshopByWorkshopId(Long workshopId) {
        return myWorkshopMapper.deleteMyWorkshopByWorkshopId(workshopId);
    }
}
